package com.flyway.utils;

import java.math.BigDecimal;

/**
 * 金额计算工具类
 *
 * @author peixinxu
 */
public class WagesUtils {

    public static BigDecimal moneyBetween(String[] betweenRangeArr, String[] betweenMoneyArr, Integer num) {
        //初始化区间值
        Integer signTempCount = 0;
        //初始化区间差
        Integer signInRang;
        //初始化金额
        BigDecimal money = BigDecimal.ZERO;
        for (int i = 0; i < betweenRangeArr.length; i++) {
            Integer gCount = Integer.valueOf(betweenRangeArr[i]);
            if (i == (betweenRangeArr.length - 1) && num > gCount) {
                // 大于
                signInRang = num - gCount;
            } else {
                // 小于等于
                if (num <= gCount) {
                    signInRang = num - signTempCount;
                } else {
                    signInRang = gCount - signTempCount;
                }
            }
            // 计算区间金额
            money = money.add(new BigDecimal(betweenMoneyArr[i]).multiply(new BigDecimal(signInRang)));
            signTempCount += signInRang;
            if (num <= gCount) {
                break;
            }
        }
        return money;
    }

    /**
     * 获取新值所属的区间,包含旧值的统计,但不包含其区间已经计算过得
     *
     * @param preValue        上一次的统计
     * @param nowValue        本次新增的数量
     * @param betweenRangeArr 范围区间
     * @param betweenMoneyArr 金额区间
     * @return 结果
     */
    public static BigDecimal manyMoneyBetween(Double preValue, Double nowValue, String[] betweenRangeArr, String[] betweenMoneyArr) {
        //总数量
        Double totalCount = preValue + nowValue;
        //初始化上次的数值,每次区间计算后,数值增加,排除已经计算过得总值
        Double tempCount = preValue;
        //记录区间数量
        Double calculatInRang;
        //初始化金额
        BigDecimal resultMoney = BigDecimal.ZERO;
        //上次的阈值
        Integer lastgCount = 0;
        for (int i = 0; i < betweenRangeArr.length; i++) {
            Integer gCount = Integer.valueOf(betweenRangeArr[i]);
            if (i == (betweenRangeArr.length - 1) && totalCount > gCount) {
                Double lcount = tempCount > gCount ? tempCount : gCount;
                calculatInRang = totalCount - lcount;
            } else {
                if (preValue > gCount) {
                    continue;
                }
                Double subCount = tempCount > lastgCount ? tempCount : lastgCount;
                if (totalCount <= gCount) {
                    calculatInRang = totalCount - subCount;
                } else {
                    calculatInRang = gCount - subCount;
                }
            }
            // 计算区间金额
            resultMoney = resultMoney.add(new BigDecimal(betweenMoneyArr[i]).multiply(new BigDecimal(calculatInRang)));
            tempCount += calculatInRang;
            if (totalCount <= gCount) {
                break;
            }
            lastgCount = gCount;
        }
        return resultMoney;
    }

    public static void main(String[] args) {
        /* 0<x<=1  2元
         * 1<x<=5  3元
         * 5<x<=7  4元
         * >7      5元
         */
        String[] betweenRangeArr = new String[]{"1", "5", "7", "7"};
        String[] betweenMoneyArr = new String[]{"2", "3", "4", "5"};
        //System.out.println(moneyBetween(betweenRangeArr, betweenMoneyArr, 8));
        System.out.println(manyMoneyBetween(1D, 8D, betweenRangeArr, betweenMoneyArr));

        int x = 1;
        int y = 2;
        int z = 3;
        // System.out.println(2 + (--y));
        System.out.println((++x) + y);
//        System.out.println(x + (y++)
//        );
        //System.out.println(x + (--z));
    }
}
